﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcPaging;
using QuocHoi_TV_Frontend.Models;
using System.IO;

namespace QuocHoi_TV_Frontend.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/

        public ActionResult Index(string searchTerm, string searchField, int? limit, int? page)
        {
            if (!string.IsNullOrEmpty(searchTerm))
            {
                searchTerm = Util.VietnameseUnsign(searchTerm);
            }
            // create default Lucene search index directory
            if (!Directory.Exists(LuceneHelper._luceneDir)) Directory.CreateDirectory(LuceneHelper._luceneDir);
            

            // perform Lucene search
            List<NewsModel> _searchResults;

            _searchResults = (string.IsNullOrEmpty(searchField) ? LuceneHelper.Search(searchTerm) : LuceneHelper.Search(searchTerm, searchField)).ToList();

            if (string.IsNullOrEmpty(searchTerm) && !_searchResults.Any())
                _searchResults = LuceneHelper.GetAllIndexRecords().ToList();

            ViewData["keyword"] = searchTerm;
            int currentPageIndex = page.HasValue ? page.Value : 1;

            IList<NewsModel> result = _searchResults;
            var orderedResult = result.OrderByDescending(a => a.NewsID);
            result = result.ToPagedList(currentPageIndex, 10);

            //var list = 
            if (Request.IsAjaxRequest())
                return PartialView("_ListNews", orderedResult.ToPagedList(currentPageIndex, 10));
            else
                return View(orderedResult.ToPagedList(currentPageIndex, 10));
        }

        [HttpPost]
        public ActionResult CreateIndex()
        {
            List<News> allNews = DataModels.getAllNews();
            LuceneHelper.AddUpdateLuceneIndex(allNews);

            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult AddToIndex(long newsID)
        {
            News News = DataModels.SelectNewsOne(newsID);
            LuceneHelper.AddUpdateLuceneIndex(News);
            TempData["Result"] = "Record was added to search index successfully!";
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult ClearIndex()
        {
            if (LuceneHelper.ClearLuceneIndex())
                TempData["Result"] = "Search index was cleared successfully!";
            else
                TempData["ResultFail"] = "Index is locked and cannot be cleared, try again later or clear manually!";
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult ClearIndexRecord(int newsID)
        {
            LuceneHelper.ClearLuceneIndexRecord(newsID);
            TempData["Result"] = "Search index record was deleted successfully!";
            return RedirectToAction("Index");
        }

        public ActionResult OptimizeIndex()
        {
            LuceneHelper.Optimize();
            TempData["Result"] = "Search index was optimized successfully!";
            return RedirectToAction("Index");
        }

    }
}
